import { defineStore } from 'pinia'

const useChatStore = defineStore('chat', {
    state: () => {
        return {
            unreadCount: {
                chat: 0,
                groupChat: 0
            },
            messageList: new Array<any>(),
            /**
             * 每次进入IM页面的时候 检查此值,如果不等于当天 则自动发送 聊天消息
             */
            savedTime: loadDate()
        }
    },
    actions: {
        unreadCountClear() {
            this.unreadCount = {
                chat: 0,
                groupChat: 0
            }
        },
        unreadCountChatAdd(num: number) {
            this.unreadCount.chat += num
        },
        unreadCountChatClear() {
            this.unreadCount.chat = 0
        },
        unreadCountGroupChatAdd(num: number) {
            this.unreadCount.groupChat += num
        },
        unreadCountGroupChatClear() {
            this.unreadCount.groupChat = 0
        },
        // 更新 消息中心页面 第一条客服 消息的最新内容
        setList(data: any) {
            this.messageList[0] = data
        },
        updateLastMessage(data: any) {
            if (data.lastContent == '结束服务') return
            if (this.messageList[0]) {
                this.messageList[0].message = data.lastContent
                this.messageList[0].updateTime = data.updateTime
            }
        },
        updateIMDate(data: any) {
            this.savedTime = data
            uni.setStorageSync('IMDATE', data)
        }
    }
})

function loadDate(): string {
    return uni.getStorageSync('IMDATE')
}

export default useChatStore
